package com.scrumtrek.trainings.tdd.model;

import com.scrumtrek.trainings.tdd.IPaySystem;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Vadim
 * Date: 11.09.13
 * Time: 10:17
 * To change this template use File | Settings | File Templates.
 */
public class BookStoreService {
    private IStore<Book> store;

    public BookStoreService(IStore<Book> store) {
        this.store = store;
    }

    @Deprecated
    public BookStoreService(IPaySystem paySystem) {
        this(new Store<Book>(paySystem));
    }

    public int getTotalBooksAmount() {
        return store.getItems().size();
    }

    public void addBook(Book book) {
        store.addItem(book);
    }

    public List<String> getAuthors() {
        HashSet<String> authors = new HashSet<String>();
        for (Book book : store.getItems()) {
            authors.add(book.getAuthor());
        }
        return new ArrayList(authors);
    }

    public List<String> getTitles() {
        HashSet<String> titles = new HashSet<String>();
        for (Book book : store.getItems()) {
            titles.add(book.toString());
        }
        return new ArrayList(titles);
    }

    public List<Book> findByAuthor(String author) {
        List<Book> bookList = new ArrayList<Book>();
        for (Book book : store.getItems()) {
            if (book.getAuthor().equals(author)) {
                bookList.add(book);
            }
        }
        return bookList;
    }

    public List<Book> findByTitle(String title) {
        List<Book> bookList = new ArrayList<Book>();
        for (Book book : store.getItems()) {
            if (book.getName().equals(title)) {
                bookList.add(book);
            }
        }
        return bookList;
    }

    public Collection<Book> getBooks() {
        return store.getItems();
    }
}
